package com.csaba.connector.bha;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.csaba.connector.ServiceException;
import com.csaba.connector.bha.model.BHACustomer;
import com.csaba.connector.bha.model.BHASession;
import com.csaba.connector.model.Session;
import com.csaba.connector.service.AbstractLoginService;
import com.csaba.util.LoggerHelper;
import com.ind.simplenode.SimpleNode;

public class BHALoginService extends AbstractLoginService
{
	private final static Logger logger = LoggerHelper.getDefaultLogger();

	@Override
	public void execute( Session session ) throws ServiceException
	{
		if ( session == null || session.getCustomer() != null )
			session = new BHASession();

		final BHASession local = (BHASession) session;
		//Create login request
		final SimpleNode loginRequest = new SimpleNode();
		loginRequest.add("loginId", customer.getLoginId());
		loginRequest.add("credential", customer.getPassword());
		loginRequest.setPath("userAgentInfo.screen", "1024*768*32");
		loginRequest.setPath("userAgentInfo.jvmVersion", System.getProperty("java.version"));
		loginRequest.setPath("userAgentInfo.msJvmVersion", "");
		loginRequest.setPath("userAgentInfo.os", System.getProperty("os.name") + " " + System.getProperty("os.version")
				+ "(" + System.getProperty("os.arch") + ")");
		final String userAgent = System.getProperty("user.agent", "N/A");
		loginRequest.setPath("userAgentInfo.userAgent", userAgent);

		// call login
		final SimpleNode loginReply = local.sendRequest("Login", "login", loginRequest);

		//		boolean authenticated = loginReply.getBoolean( "authneticated" );
		final boolean mustChangePassword = loginReply.getBoolean("mustChangePassword");
		if ( mustChangePassword )
			throw new ServiceException("Your password is about to expiry.",
					"Login to your internet bank client for changing the password.");
		//		boolean recorderUser = loginReply.getBoolean( "recorderUser" );
		//		boolean tokenAuthMode = loginReply.getBoolean( "tokenAuthMode" );
		//		boolean servicesNote = loginReply.getBoolean( "servicesNote" );
		//		int timeout = loginReply.getInt( "timeout" );
		//		int maxImportFileLength = loginReply.getInt( "maxImportFileLength" );
		//		int loginStatusId = loginReply.exists( "loginStatusId" ) ? loginReply.getInt( "loginReplyId" ) : 0;
		final boolean blocked = loginReply.getBoolean("blocked");
		if ( blocked )
		{
			throw new ServiceException("User " + customer.getLoginId() + " is blocked.");
		}
		final String userName = loginReply.getString("loginUserName");
		final SimpleNode[] customers = loginReply.getNodeArray("customers");

		// select customer
		final SimpleNode setCustomerRequest = new SimpleNode();
		setCustomerRequest.add("selectedCustomerId", customers[0].getString("customerId")); //TODO always the first customer Id is used. No cap selection is possible.
		final SimpleNode setCustomerReply = local.sendRequest("Login", "setCustomerId", setCustomerRequest);
		final SimpleNode[] functions = setCustomerReply.getNodeArray("functions");

		if ( logger.isLoggable(Level.FINE) )
		{
			logger.fine("Following functions are enabled:");
			for ( final SimpleNode function : functions )
			{
				logger.fine(" - " + function.getString("functionId") + " (authentication "
						+ ( function.getBoolean("needAuthorization") ? "is" : "isn't" ) + " required)");
			}
		}

		local.setCustomer(new BHACustomer(customer, setCustomerReply.getNode("customerData")));
		logger.info("User " + userName + " is logging in with customer " + local.getCustomer().getName() + " ("
				+ local.getCustomer().getId() + ")...");

		this.session = session;
	}
}
